import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog, QPushButton, QVBoxLayout, QLabel, QFontDialog, QColorDialog, QComboBox
from PyQt5.QtGui import QFont, QColor
from PyQt5.QtCore import Qt

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('主窗体')

        # 创建一个目标控件（例如，标签）
        self.target_label = QLabel('这是一个目标控件', self)
        self.target_label.setAlignment(Qt.AlignCenter)

        # 创建字体选择下拉框
        self.font_combobox = QComboBox(self)
        self.font_combobox.currentIndexChanged.connect(self.updateFontLabel)
        self.font_combobox.setEditable(False)
        self.font_combobox.addItem("选择字体")

        # 创建颜色选择下拉框
        self.color_combobox = QComboBox(self)
        self.color_combobox.currentIndexChanged.connect(self.updateColorLabel)
        self.color_combobox.setEditable(False)
        self.color_combobox.addItem("选择颜色")

        # 创建一个按钮，点击后弹出设置对话框
        set_button = QPushButton('设置', self)
        set_button.clicked.connect(self.showSettingDialog)

    def showSettingDialog(self):
        dialog = SettingDialog(self)
        result = dialog.exec_()
        if result == QDialog.Accepted:
            # 用户点击了对话框的“应用”按钮，应用设置到目标控件
            font = dialog.getSelectedFont()
            color = dialog.getSelectedColor()

            self.target_label.setFont(font)
            self.target_label.setStyleSheet(f'color: {color.name()};')

    def updateFontLabel(self, font_index):
        if font_index > 0:
            font = QFont(self.font_combobox.itemText(font_index))
            self.font_combobox.setItemText(0, font.family())
        else:
            self.font_combobox.setItemText(0, "选择字体")

    def updateColorLabel(self, color_index):
        if color_index > 0:
            selected_color = QColor(self.color_combobox.itemText(color_index))
            self.color_combobox.setItemText(0, selected_color.name())
        else:
            self.color_combobox.setItemText(0, "选择颜色")

class SettingDialog(QDialog):
    def __init__(self, parent):
        super().__init__(parent)
        self.selectedFont = None
        self.selectedColor = None
        self.initUI()

    def initUI(self):
        self.setWindowTitle('设置对话框')

        font_combobox = QComboBox(self)
        font_combobox.setEditable(True)
        font_combobox.addItem("选择字体")
        font_combobox.activated.connect(self.setFont)

        color_combobox = QComboBox(self)
        color_combobox.setEditable(True)
        color_combobox.addItem("选择颜色")
        color_combobox.activated.connect(self.setColor)

        apply_button = QPushButton('应用', self)
        apply_button.clicked.connect(self.accept)

        cancel_button = QPushButton('取消', self)
        cancel_button.clicked.connect(self.reject)

        layout = QVBoxLayout()
        layout.addWidget(font_combobox)
        layout.addWidget(color_combobox)
        layout.addWidget(apply_button)
        layout.addWidget(cancel_button)
        self.setLayout(layout)

    def setFont(self, font_index):
        if font_index > 0:
            font = QFont(self.sender().currentText())
            self.selectedFont = font

    def setColor(self, color_index):
        if color_index > 0:
            selected_color = QColor(self.sender().currentText())
            self.selectedColor = selected_color

    def getSelectedFont(self):
        return self.selectedFont

    def getSelectedColor(self):
        return self.selectedColor

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())
